// https://market.cloud.tencent.com/products/7494
// https://www.npmjs.com/package/crypto-js
// const CryptoJS = require("crypto-js");
import HmacSHA1 from 'crypto-js/hmac-sha1';
import Base64 from 'crypto-js/enc-base64';

export async function searchByCrypto(isbn: string | number): any {
  // 云市场分配的密钥Id
  const secretId = "AKIDkc0E796TxgYJ2tHOZAA8exDYK2hk18VT2dI6";
  // 云市场分配的密钥Key
  const secretKey = "hek5Vvo3092t5x3l0l7a1n7xxO1ta58xtpv178po";
  const source = "market";

  // 签名
  const datetime = (new Date()).toGMTString();
  const signStr = "x-date: " + datetime + "\n" + "x-source: " + source;
  const sign = Base64.stringify(HmacSHA1(signStr, secretKey))
  const auth = 'hmac id="' + secretId + '", algorithm="hmac-sha1", headers="x-date x-source", signature="' + sign + '"';

  const url = `/myqcloud/release/isbn?isbn=${isbn}`;
  // const url = `/myqcloud/release/isbn?isbn=9787208061644`;
  const options = {
    timeout: 5000,
    method: "GET",
    headers: {
      "X-Source": source,
      "X-Date": datetime,
      "Authorization": auth,
    }
  }
  console.log(url, options);
  
  const data = await (await fetch(url, options)).json();
  console.log('data', data);
  
  return data?.showapi_res_body;
}
